package com.bk.servlet;

import com.bk.pojo.Book;
import com.bk.service.BookService;
import com.bk.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 27435
 * @Date 2024/11/2613:26
 * @Version 1.0
 */

@WebServlet(urlPatterns = {"/updateBook"})
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求编码
        req.setCharacterEncoding("UTF-8");
        // 设置响应编码
        resp.setCharacterEncoding("UTF-8");
        // 设置响应类型
        resp.setContentType("text/html;charset=UTF-8");
        // 响应流
        PrintWriter out = resp.getWriter();
        // 获取id
        String id = req.getParameter("id");
        // 获取name
        String name = req.getParameter("name");
        // 获取author
        String author = req.getParameter("author");
        // 获取price
        String price = req.getParameter("price");
        // 获取typeId
        String typeId = req.getParameter("typeId");
        // 实例化Book对象
        Book book = new Book(Integer.valueOf(id), name, author, Float.valueOf(price), Integer.valueOf(typeId));
        // 调用业务层查询所有图书的方法
        BookService bookService = new BookServiceImpl();
        // 根据id修改图书
        int i = bookService.updateBookById(book);
        // 判断是否修改成功
        if (i > 0) {
            out.print("修改成功");
        }else {
            out.print("修改失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
